/**
 * 处理重借款JS
 */

//变更币种后，如果单据有重借款，则全清空
function resetCJK4BzbmChange(){
	if(isLoad){
		isLoad = false;
		return;   
	}
	var cDs = getDataset("bx_contrast");
	if(cDs == null || cDs.getRowCount() == 0)
		return ;
	clearContrastDs();
	
	if(getParameter('type') == 'hk'){
		setBxzbJe(0, 0);//设置主表金额
	}else{
		setBxFinJe(0, 0);//设置财务页签金额
	}
	setParam('Y');
}

/** 
 * 清空冲销Ds
 */
function clearContrastDs(){
	var cDs = getDataset('bx_contrast');
	cDs.deleteRows(-1);
}



/**
* 设置冲销页签数据
* ds,重借款页面的Ds
*/
function setContrast(ds,cjkRows)
{ 
	var cDs = getDataset('bx_contrast');
	if(cjkRows == null || cjkRows.length == 0){//如果参数为null则全删 冲销表
		cDs.deleteRows(-1);
		return;
	}
	//必须设
	var cjkybjeIndex = cDs.nameToIndex('cjkybje');
	var pk_bxdIndex = cDs.nameToIndex('pk_bxd');
	var pk_jkdIndex = cDs.nameToIndex('pk_jkd');
	
	var ybjeIndex = cDs.nameToIndex('ybje');
	var bbjeIndex = cDs.nameToIndex('bbje');
	var fbjeIndex = cDs.nameToIndex('fbje');
	var cjkbbjeIndex = cDs.nameToIndex('cjkbbje');
	var cjkfbjeIndex = cDs.nameToIndex('cjkfbje');
	var deptidIndex = cDs.nameToIndex('deptid');
	var djlxbmIndex = cDs.nameToIndex('djlxbm');
	var jkbxrIndex = cDs.nameToIndex('jkbxr');
	var jobidIndex = cDs.nameToIndex('jobid');
	var pk_corpIndex = cDs.nameToIndex('pk_corp');
	var fyybjeIndex = cDs.nameToIndex('fyybje');
	var szxmidIndex = cDs.nameToIndex('szxmid');
	var cxrqIndex = cDs.nameToIndex('cxrq');

	var cxMap = new HashMap();
	var cxParamMap = new HashMap();
	
	var tmpArr = new Array();
	//将原有的冲销明细放入map
	for(var i=0,cDscount = cDs.getRowCount(); i<cDscount; i++){
		var row = cDs.getRow(i);
		var key = row.getCellValue(pk_jkdIndex)//pk_jkbx;
		if(cxMap.containsKey(key)){
			tmpArr.push(i)
			continue;
		}
		cxMap.put(key,row);
		cxParamMap.put(key,i);
	}
	/*如果是打开的单据，可能有pk_jkd一直的行，所以需要删掉*/
	if(tmpArr != null && tmpArr.length != 0)
		cDs.deleteRows(tmpArr);
	
	for(var i=0,count = cjkRows.length; i<count; i++){
		var pk_jkbx = cjkRows[i].getCellValue(ds.nameToIndex('pk_jkbx'));
		if(cxMap.containsKey(pk_jkbx)){
			$updateContrast(ds,cjkRows[i],cxParamMap.get(pk_jkbx));//update
			cxMap.remove(pk_jkbx);
		}
		else
			$updateContrast(ds,cjkRows[i],null);//insert
	}
	var delArr = new Array();
	for(var i=0,cDscount = cDs.getRowCount(); i<cDscount; i++){
		var key = cDs.getRow(i).getCellValue(pk_jkdIndex)//pk_jkbx;
		if(cxMap.containsKey(key))
			delArr.push(i);
	}
	if(delArr != null && delArr.length != 0)
		cDs.deleteRows(delArr);
};
//更新冲销明细表
function $updateContrast(ds ,cjkrow,cdsrowIndex){
	var cDs = getDataset('bx_contrast');
	var headDs = getDataset('bx_bxzb');
	
	var cjkybjeIndex = cDs.nameToIndex('cjkybje');
	var pk_bxdIndex = cDs.nameToIndex('pk_bxd');
	var pk_jkdIndex = cDs.nameToIndex('pk_jkd');
	
	var ybjeIndex = cDs.nameToIndex('ybje');
	var bbjeIndex = cDs.nameToIndex('bbje');
	var fbjeIndex = cDs.nameToIndex('fbje');
	var cjkbbjeIndex = cDs.nameToIndex('cjkbbje');
	var cjkfbjeIndex = cDs.nameToIndex('cjkfbje');
	var deptidIndex = cDs.nameToIndex('deptid');
	var djlxbmIndex = cDs.nameToIndex('djlxbm');
	var jkbxrIndex = cDs.nameToIndex('jkbxr');
	var jobidIndex = cDs.nameToIndex('jobid');
	var pk_corpIndex = cDs.nameToIndex('pk_corp');
	var fyybjeIndex = cDs.nameToIndex('fyybje');
	var szxmidIndex = cDs.nameToIndex('szxmid');
	var cxrqIndex = cDs.nameToIndex('cxrq');
	var sxrqIndex = cDs.nameToIndex('sxrq');
	var sxbzIndex = cDs.nameToIndex('sxbz');
	var tsIndex = cDs.nameToIndex('ts');
	var je = cjkrow.getCellValue(ds.nameToIndex('zyx2'));
	
	if(cdsrowIndex == null){//新增的
		var row = cDs.addEmptyRow();
		cdsrowIndex = cDs.getRowIndex(row);
	}
	
	cDs.setValueAt(cdsrowIndex,ybjeIndex,je);//原币金额
	cDs.setValueAt(cdsrowIndex,cjkybjeIndex,je);//冲借款原币
	var hkybje = parseFloat(cjkrow.getCellValue(ds.nameToIndex('hkybje')));
	cDs.setValueAt(cdsrowIndex,fyybjeIndex,je - hkybje,true)//费用
		
	cDs.setValueAt(cdsrowIndex,cxrqIndex,getLoginDate());//冲销日期
	var pk_jkbx = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('pk_jkbx'));//报销单的pk
	cDs.setValueAt(cdsrowIndex,pk_bxdIndex,pk_jkbx);//报销单建
	cDs.setValueAt(cdsrowIndex,pk_jkdIndex,cjkrow.getCellValue(ds.nameToIndex('pk_jkbx')));//借款单
	cDs.setValueAt(cdsrowIndex,deptidIndex,cjkrow.getCellValue(ds.nameToIndex('deptid')),true)//部门
	cDs.setValueAt(cdsrowIndex,jobidIndex,cjkrow.getCellValue(ds.nameToIndex('jobid')),true)//项目
	cDs.setValueAt(cdsrowIndex,jkbxrIndex,cjkrow.getCellValue(ds.nameToIndex('jkbxr')),true)//借款人
	cDs.setValueAt(cdsrowIndex,pk_corpIndex,cjkrow.getCellValue(ds.nameToIndex('zfdwbm')),true)//公司
	cDs.setValueAt(cdsrowIndex,szxmidIndex,cjkrow.getCellValue(ds.nameToIndex('szxmid')),true)//收支项目
	
	cDs.setValueAt(cdsrowIndex,sxbzIndex,0);//生效标识【未生效】
	cDs.setValueAt(cdsrowIndex,sxrqIndex,null);//生效日期
	cDs.setValueAt(cdsrowIndex,tsIndex,cjkrow.getCellValue(ds.nameToIndex('ts')));//TS
	
	var arr = $$getKjArr();
	cDs.setValueAt(cdsrowIndex,cDs.nameToIndex('cxnd'),arr[0])//会计年度
	cDs.setValueAt(cdsrowIndex,cDs.nameToIndex('cxqj'),arr[1])//会计期间
	
	//增加‘借款单据编号’
	cDs.setValueAt(cdsrowIndex,cDs.nameToIndex('jkdjbh'),cjkrow.getCellValue(ds.nameToIndex('djbh')));
	var bxdjbh = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('djbh'));//主表的單據編號
	cDs.setValueAt(cdsrowIndex,cDs.nameToIndex('bxdjbh'),bxdjbh);
	//单据类型编码
	var djlxbm = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('djlxbm'));
	cDs.setValueAt(cdsrowIndex,cDs.nameToIndex('djlxbm'),djlxbm);
};

/*v[0]=会计年度、v[1]=会计期间*/
function $$getKjArr(){
	var lDate = getLoginDate();
	var arr = [lDate.substring(0,4),parseInt(lDate.substring(5,7))]
	return arr;
}